home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / P028RH (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  12.7 KB  |  334 lines

  1. package com.sun.java.swing.plaf.metal;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.Action;
  5. import com.sun.java.swing.BorderFactory;
  6. import com.sun.java.swing.Box;
  7. import com.sun.java.swing.BoxLayout;
  8. import com.sun.java.swing.JButton;
  9. import com.sun.java.swing.JComboBox;
  10. import com.sun.java.swing.JComponent;
  11. import com.sun.java.swing.JLabel;
  12. import com.sun.java.swing.JList;
  13. import com.sun.java.swing.JPanel;
  14. import com.sun.java.swing.JScrollPane;
  15. import com.sun.java.swing.JTextField;
  16. import com.sun.java.swing.JToggleButton;
  17. import com.sun.java.swing.event.ListSelectionEvent;
  18. import com.sun.java.swing.plaf.ComponentUI;
  19. import com.sun.java.swing.plaf.basic.BasicFileChooserUI;
  20. import com.sun.java.swing.preview.JFileChooser;
  21. import java.awt.BorderLayout;
  22. import java.awt.Container;
  23. import java.awt.Dimension;
  24. import java.awt.Insets;
  25. import java.beans.PropertyChangeEvent;
  26. import java.io.File;
  27.  
  28. public class MetalFileChooserUI extends BasicFileChooserUI {
  29.    private JPanel centerPanel;
  30.    private JComboBox directoryComboBox;
  31.    private DirectoryComboBoxModel directoryComboBoxModel;
  32.    private Action directoryComboBoxAction = new DirectoryComboBoxAction(this);
  33.    private FilterComboBoxModel filterComboBoxModel;
  34.    private JTextField filenameTextField;
  35.    private JList list;
  36.    private JButton approveButton;
  37.    private JButton cancelButton;
  38.    private JComboBox filterComboBox;
  39.    private JPanel bodyPanel = null;
  40.    private static final Dimension hstrut10 = new Dimension(10, 1);
  41.    private static final Dimension hstrut25 = new Dimension(25, 1);
  42.    private static final Dimension vstrut10 = new Dimension(1, 10);
  43.    private static final Insets shrinkwrap = new Insets(0, 0, 0, 0);
  44.    private static int PREF_WIDTH = 500;
  45.    private static int PREF_HEIGHT = 300;
  46.    private static Dimension PREF_SIZE;
  47.    private static int MIN_WIDTH;
  48.    private static int MIN_HEIGHT;
  49.    private static Dimension MIN_SIZE;
  50.    private static int LIST_MIN_WIDTH;
  51.    private static int LIST_MIN_HEIGHT;
  52.    private static Dimension LIST_MIN_SIZE;
  53.  
  54.    static {
  55.       PREF_SIZE = new Dimension(PREF_WIDTH, PREF_HEIGHT);
  56.       MIN_WIDTH = 400;
  57.       MIN_HEIGHT = 200;
  58.       MIN_SIZE = new Dimension(MIN_WIDTH, MIN_HEIGHT);
  59.       LIST_MIN_WIDTH = 400;
  60.       LIST_MIN_HEIGHT = 100;
  61.       LIST_MIN_SIZE = new Dimension(LIST_MIN_WIDTH, LIST_MIN_HEIGHT);
  62.    }
  63.  
  64.    public MetalFileChooserUI(JFileChooser filechooser) {
  65.       super(filechooser);
  66.    }
  67.  
  68.    protected DirectoryComboBoxModel createDirectoryComboBoxModel() {
  69.       return new DirectoryComboBoxModel(this);
  70.    }
  71.  
  72.    protected DirectoryComboBoxRenderer createDirectoryComboBoxRenderer() {
  73.       return new DirectoryComboBoxRenderer(this);
  74.    }
  75.  
  76.    protected FilterComboBoxModel createFilterComboBoxModel() {
  77.       return new FilterComboBoxModel(this);
  78.    }
  79.  
  80.    protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
  81.       return new FilterComboBoxRenderer(this);
  82.    }
  83.  
  84.    protected JPanel createList() {
  85.       JPanel p = new JPanel(new BorderLayout());
  86.       this.list = new JList();
  87.       this.list.setCellRenderer(new FileRenderer(this));
  88.       this.list.setModel(super.model);
  89.       this.list.addListSelectionListener(((BasicFileChooserUI)this).createListSelectionListener());
  90.       this.list.addMouseListener(((BasicFileChooserUI)this).createDoubleClickListener(this.list));
  91.       JScrollPane scrollpane = new JScrollPane(this.list);
  92.       ((JComponent)scrollpane).setBorder(BorderFactory.createLoweredBevelBorder());
  93.       ((Container)p).add(scrollpane, "Center");
  94.       return p;
  95.    }
  96.  
  97.    public static ComponentUI createUI(JComponent c) {
  98.       return new MetalFileChooserUI((JFileChooser)c);
  99.    }
  100.  
  101.    public void ensureFileIsVisible(File f) {
  102.       if (super.model.contains(f)) {
  103.          this.list.setSelectedIndex(super.model.indexOf(f));
  104.          this.list.ensureIndexIsVisible(this.list.getSelectedIndex());
  105.       }
  106.  
  107.    }
  108.  
  109.    public String getDirectoryName() {
  110.       return null;
  111.    }
  112.  
  113.    public String getFileName() {
  114.       return this.filenameTextField != null ? this.filenameTextField.getText() : null;
  115.    }
  116.  
  117.    public Dimension getMaximumSize(JComponent x) {
  118.       return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  119.    }
  120.  
  121.    public Dimension getMinimumSize(JComponent x) {
  122.       return MIN_SIZE;
  123.    }
  124.  
  125.    public Dimension getPreferredSize(JComponent x) {
  126.       return PREF_SIZE;
  127.    }
  128.  
  129.    public void installComponents() {
  130.       ((BasicFileChooserUI)this).getFileChooser().setLayout(new BoxLayout(((BasicFileChooserUI)this).getFileChooser(), 1));
  131.       ((BasicFileChooserUI)this).getFileChooser().add(Box.createRigidArea(vstrut10));
  132.       JPanel topPanel = new JPanel();
  133.       ((Container)topPanel).setLayout(new BoxLayout(topPanel, 0));
  134.       ((BasicFileChooserUI)this).getFileChooser().add(topPanel);
  135.       ((BasicFileChooserUI)this).getFileChooser().add(Box.createRigidArea(vstrut10));
  136.       JLabel l = new JLabel("Look in:");
  137.       ((JComponent)l).setAlignmentX(0.0F);
  138.       ((JComponent)l).setAlignmentY(0.5F);
  139.       ((Container)topPanel).add(Box.createRigidArea(hstrut10));
  140.       ((Container)topPanel).add(l);
  141.       ((Container)topPanel).add(Box.createRigidArea(hstrut25));
  142.       this.directoryComboBox = new JComboBox();
  143.       this.directoryComboBoxModel = this.createDirectoryComboBoxModel();
  144.       this.directoryComboBox.setModel(this.directoryComboBoxModel);
  145.       this.directoryComboBox.addActionListener(this.directoryComboBoxAction);
  146.       this.directoryComboBox.setRenderer(this.createDirectoryComboBoxRenderer());
  147.       this.directoryComboBox.setAlignmentX(0.0F);
  148.       this.directoryComboBox.setAlignmentY(0.5F);
  149.       ((Container)topPanel).add(this.directoryComboBox);
  150.       ((Container)topPanel).add(Box.createRigidArea(hstrut10));
  151.       JButton b = new JButton(super.upFolderIcon);
  152.       ((JComponent)b).setToolTipText("Up One Level");
  153.       ((JComponent)b).setAlignmentX(0.0F);
  154.       ((JComponent)b).setAlignmentY(0.5F);
  155.       ((AbstractButton)b).setMargin(shrinkwrap);
  156.       ((AbstractButton)b).setFocusPainted(false);
  157.       ((AbstractButton)b).addActionListener(((BasicFileChooserUI)this).getChangeToParentDirectoryAction());
  158.       ((Container)topPanel).add(b);
  159.       ((Container)topPanel).add(Box.createRigidArea(hstrut10));
  160.       b = new JButton(super.homeFolderIcon);
  161.       ((JComponent)b).setToolTipText("Home");
  162.       ((JComponent)b).setAlignmentX(0.0F);
  163.       ((JComponent)b).setAlignmentY(0.5F);
  164.       ((AbstractButton)b).setMargin(shrinkwrap);
  165.       ((AbstractButton)b).setFocusPainted(false);
  166.       ((AbstractButton)b).addActionListener(((BasicFileChooserUI)this).getGoHomeAction());
  167.       ((Container)topPanel).add(b);
  168.       ((Container)topPanel).add(Box.createRigidArea(hstrut10));
  169.       b = new JButton(super.newFolderIcon);
  170.       ((JComponent)b).setToolTipText("Create New Folder");
  171.       ((JComponent)b).setAlignmentX(0.0F);
  172.       ((JComponent)b).setAlignmentY(0.5F);
  173.       ((AbstractButton)b).setMargin(shrinkwrap);
  174.       ((AbstractButton)b).setFocusPainted(false);
  175.       ((AbstractButton)b).addActionListener(((BasicFileChooserUI)this).getNewFolderAction());
  176.       ((Container)topPanel).add(b);
  177.       ((Container)topPanel).add(Box.createRigidArea(hstrut10));
  178.       JToggleButton tb = new JToggleButton(super.listViewIcon);
  179.       ((JComponent)tb).setToolTipText("List");
  180.       ((AbstractButton)tb).setEnabled(false);
  181.       ((AbstractButton)tb).setFocusPainted(false);
  182.       ((JComponent)tb).setAlignmentX(0.0F);
  183.       ((JComponent)tb).setAlignmentY(0.5F);
  184.       ((AbstractButton)tb).setMargin(shrinkwrap);
  185.       ((Container)topPanel).add(tb);
  186.       tb = new JToggleButton(super.detailsViewIcon);
  187.       ((JComponent)tb).setToolTipText("Details");
  188.       ((AbstractButton)tb).setFocusPainted(false);
  189.       ((AbstractButton)tb).setSelected(true);
  190.       ((AbstractButton)tb).setEnabled(false);
  191.       ((JComponent)tb).setAlignmentX(0.0F);
  192.       ((JComponent)tb).setAlignmentY(0.5F);
  193.       ((AbstractButton)tb).setMargin(shrinkwrap);
  194.       ((Container)topPanel).add(tb);
  195.       ((Container)topPanel).add(Box.createRigidArea(hstrut10));
  196.       this.centerPanel = new JPanel(new BorderLayout());
  197.       JPanel p = this.createList();
  198.       ((JComponent)p).setMinimumSize(LIST_MIN_SIZE);
  199.       this.centerPanel.add(p, "Center");
  200.       this.centerPanel.add(((BasicFileChooserUI)this).getAccessoryPanel(), "East");
  201.       JComponent accessory = ((BasicFileChooserUI)this).getFileChooser().getAccessory();
  202.       if (accessory != null) {
  203.          ((BasicFileChooserUI)this).getAccessoryPanel().add(accessory);
  204.       }
  205.  
  206.       ((BasicFileChooserUI)this).getFileChooser().add(this.centerPanel);
  207.       JPanel bottomPanel = new JPanel();
  208.       ((Container)bottomPanel).setLayout(new BoxLayout(bottomPanel, 0));
  209.       ((Container)bottomPanel).add(Box.createRigidArea(hstrut10));
  210.       ((BasicFileChooserUI)this).getFileChooser().add(Box.createRigidArea(vstrut10));
  211.       ((BasicFileChooserUI)this).getFileChooser().add(bottomPanel);
  212.       ((BasicFileChooserUI)this).getFileChooser().add(Box.createRigidArea(vstrut10));
  213.       JPanel labelPanel = new JPanel();
  214.       ((Container)labelPanel).setLayout(new BoxLayout(labelPanel, 1));
  215.       l = new JLabel("File name:");
  216.       ((Container)labelPanel).add(l);
  217.       ((Container)labelPanel).add(Box.createRigidArea(vstrut10));
  218.       l = new JLabel("Files of type:");
  219.       ((Container)labelPanel).add(l);
  220.       ((Container)bottomPanel).add(labelPanel);
  221.       ((Container)bottomPanel).add(Box.createRigidArea(hstrut25));
  222.       JPanel fileAndFilterPanel = new JPanel();
  223.       ((Container)fileAndFilterPanel).setLayout(new BoxLayout(fileAndFilterPanel, 1));
  224.       this.filenameTextField = new JTextField();
  225.       this.filenameTextField.addActionListener(((BasicFileChooserUI)this).getApproveSelectionAction());
  226.       File f = ((BasicFileChooserUI)this).getFileChooser().getSelectedFile();
  227.       if (f != null) {
  228.          this.setFileName(((BasicFileChooserUI)this).getFileChooser().getName(f));
  229.       }
  230.  
  231.       ((Container)fileAndFilterPanel).add(this.filenameTextField);
  232.       ((Container)fileAndFilterPanel).add(Box.createRigidArea(vstrut10));
  233.       this.filterComboBoxModel = this.createFilterComboBoxModel();
  234.       ((BasicFileChooserUI)this).getFileChooser().addPropertyChangeListener(this.filterComboBoxModel);
  235.       this.filterComboBox = new JComboBox(this.filterComboBoxModel);
  236.       this.filterComboBox.setRenderer(this.createFilterComboBoxRenderer());
  237.       ((Container)fileAndFilterPanel).add(this.filterComboBox);
  238.       ((Container)bottomPanel).add(fileAndFilterPanel);
  239.       ((Container)bottomPanel).add(Box.createRigidArea(hstrut10));
  240.       JPanel buttonPanel = new JPanel();
  241.       ((Container)buttonPanel).setLayout(new BoxLayout(buttonPanel, 1));
  242.       this.approveButton = new 1(((BasicFileChooserUI)this).getApproveButtonText());
  243.       this.approveButton.addActionListener(((BasicFileChooserUI)this).getApproveSelectionAction());
  244.       this.approveButton.setToolTipText(((BasicFileChooserUI)this).getApproveButtonToolTipText());
  245.       ((Container)buttonPanel).add(this.approveButton);
  246.       ((Container)buttonPanel).add(Box.createRigidArea(vstrut10));
  247.       this.cancelButton = new 2(super.cancelButtonText);
  248.       this.cancelButton.setToolTipText(BasicFileChooserUI.cancelButtonToolTipText);
  249.       this.cancelButton.addActionListener(((BasicFileChooserUI)this).getCancelSelectionAction());
  250.       ((Container)buttonPanel).add(this.cancelButton);
  251.       ((Container)bottomPanel).add(buttonPanel);
  252.       ((Container)bottomPanel).add(Box.createRigidArea(hstrut10));
  253.    }
  254.  
  255.    public void installUI(JComponent c) {
  256.       super.installUI(c);
  257.    }
  258.  
  259.    public void propertyChange(PropertyChangeEvent e) {
  260.       String prop = e.getPropertyName();
  261.       if (prop.equals("ApproveSelection")) {
  262.          File f = (File)e.getNewValue();
  263.          if (f != null) {
  264.             this.setFileName(((BasicFileChooserUI)this).getFileChooser().getName(f));
  265.             if (super.model.contains(f)) {
  266.                this.list.setSelectedIndex(super.model.indexOf(e.getNewValue()));
  267.                this.list.ensureIndexIsVisible(this.list.getSelectedIndex());
  268.             }
  269.          }
  270.       } else if (prop.equals("directoryChanged")) {
  271.          super.fileView.clearIconCache();
  272.          this.list.clearSelection();
  273.          File currentDirectory = ((BasicFileChooserUI)this).getFileChooser().getCurrentDirectory();
  274.          if (currentDirectory != null) {
  275.             this.directoryComboBoxModel.addItem(currentDirectory);
  276.             ((BasicFileChooserUI)this).getNewFolderAction().setEnabled(currentDirectory.canWrite());
  277.          }
  278.       } else if (prop.equals("fileSelectionChanged")) {
  279.          super.fileView.clearIconCache();
  280.          this.list.clearSelection();
  281.       } else if (prop == "AccessoryChangedProperty") {
  282.          if (super.accessoryPanel != null) {
  283.             if (e.getOldValue() != null) {
  284.                ((BasicFileChooserUI)this).getAccessoryPanel().remove((JComponent)e.getOldValue());
  285.             }
  286.  
  287.             JComponent accessory = (JComponent)e.getNewValue();
  288.             if (accessory != null) {
  289.                ((BasicFileChooserUI)this).getAccessoryPanel().add(accessory, "Center");
  290.             }
  291.          }
  292.       } else if (prop == "ApproveButtonTextChangedProperty" || prop == "DialogTypeChangedProperty") {
  293.          this.approveButton.setText(((BasicFileChooserUI)this).getApproveButtonText());
  294.          this.approveButton.setToolTipText(((BasicFileChooserUI)this).getApproveButtonToolTipText());
  295.       }
  296.  
  297.    }
  298.  
  299.    public void rescanCurrentDirectory() {
  300.       super.model.invalidateFileCache();
  301.       super.model.validateFileCache();
  302.    }
  303.  
  304.    public void setDirectoryName(String dirname) {
  305.    }
  306.  
  307.    public void setFileName(String filename) {
  308.       if (this.filenameTextField != null) {
  309.          this.filenameTextField.setText(filename);
  310.       }
  311.  
  312.    }
  313.  
  314.    public void uninstallUI(JComponent c) {
  315.       ((BasicFileChooserUI)this).getFileChooser().removePropertyChangeListener(this.filterComboBoxModel);
  316.       this.cancelButton.removeActionListener(((BasicFileChooserUI)this).getCancelSelectionAction());
  317.       this.approveButton.removeActionListener(((BasicFileChooserUI)this).getApproveSelectionAction());
  318.       this.filenameTextField.removeActionListener(((BasicFileChooserUI)this).getApproveSelectionAction());
  319.       super.uninstallUI(c);
  320.    }
  321.  
  322.    public void valueChanged(ListSelectionEvent e) {
  323.       File f = ((BasicFileChooserUI)this).getFileChooser().getSelectedFile();
  324.       if (!e.getValueIsAdjusting() && f != null && !((BasicFileChooserUI)this).getFileChooser().isTraversable(f)) {
  325.          this.setFileName(((BasicFileChooserUI)this).getFileChooser().getName(f));
  326.       }
  327.  
  328.    }
  329.  
  330.    static JComboBox access$directoryComboBox(MetalFileChooserUI var0) {
  331.       return var0.directoryComboBox;
  332.    }
  333. }
  334.